import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Java_1 {

    public static void main(String[] args) throws SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.println("说话者：");
        String str1=sc.nextLine();
        System.out.println("被说话者:");
        String str2=sc.nextLine();
        System.out.println("内容:");
        String str3=sc.nextLine();

        //1.创建数据源
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/messgage?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1113");

        //2.和服务器构建联系
        Connection connection= (Connection) dataSource.getConnection();

        //构造SQL语句
        String sql="insert into staid values(?,?,?)";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setString(1,str1);
        statement.setString(2,str2);
        statement.setString(3,str3);

        int n= statement.executeUpdate();
        System.out.println("n："+n);

        statement.close();;
        connection.close();
    }

}
